<?php
/**
* @file $Id: ReportCards.php 548 2007-06-04 05:09:00Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if(count($_REQUEST['mp_arr']))
{
	foreach($_REQUEST['mp_arr'] as $mp)
		$mp_list .= ",'$mp'";
	$mp_list = substr($mp_list,1);
	$last_mp = $mp;
}
$attendance_codes = DBGet(DBQuery("SELECT SHORT_NAME,ID FROM ATTENDANCE_CODES WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND (DEFAULT_CODE!='Y' OR DEFAULT_CODE IS NULL) AND TABLE_NAME='0'"));

$extra['second_col'] .= '<fieldset><legend>'._('Include on Report Card').'</legend><TABLE>';

$extra['second_col'] .= '<TR><TD colspan=2><TABLE>';
	$extra['second_col'] .= '<TR>';
	$extra['second_col'] .= '<TD><INPUT type=checkbox name=elements[grade] value=Y CHECKED>'._('Letter Grade').'</TD>';
	$extra['second_col'] .= '<TD><INPUT type=checkbox name=elements[percent] value=Y CHECKED>'._('Percent').'</TD>';
	$extra['second_col'] .= '</TR><TR>';
	$extra['second_col'] .= '<TD><INPUT type=checkbox name=elements[gpa] value=Y >'._('GPA').'</TD>';
	if(SystemPreferences('WEIGHTED_GPA')=='Y')
		$extra['second_col'] .= '<TD><INPUT type=checkbox name=elements[weighted_gpa] value=Y >'._('Weighted GPA').'</TD>';
	$extra['second_col'] .= '</TR><TR>';
	$extra['second_col'] .= '<TD colspan=2><INPUT type=checkbox name=elements[average_percent_grade] value=Y >'._('Average Percent Grade').'</TD>';
	$extra['second_col'] .= '</TR><TR>';
	$extra['second_col'] .= '<TD colspan=2><INPUT type=checkbox name=elements[period_absences] value=Y CHECKED>'._('Period-by-Period Absences').'<BR><BR></TD>';
	$extra['second_col'] .= '</TR><TR>';
	$extra['second_col'] .= '<TD><INPUT type=checkbox name=elements[comments] value=Y CHECKED>'._('Comments').'<BR><BR></TD>';
	$extra['second_col'] .= '<TD><INPUT type=checkbox name=elements[ilp] value=Y >'._('Teacher MP Comments').'<BR><BR></TD>';
	$extra['second_col'] .= '</TR><TR>';
	$extra['second_col'] .= '<TD colspan=2><INPUT type=checkbox name=elements[teacher] value=Y CHECKED>'._('Teacher').'<BR><BR></TD>';
	$extra['second_col'] .= '</TR><TR>';
	$extra['second_col'] .= '<TD colspan=2><INPUT type=checkbox name=elements[ytd_absences] value=Y >'._('Year-to-date Daily Absences').'</TD>';
	$extra['second_col'] .= '</TR><TR>';
	$extra['second_col'] .= '<TD colspan=2><INPUT type=checkbox name=elements[mp_absences] value=Y >'._('Daily Absences this quarter').'</TD>';
	$extra['second_col'] .= '</TR><TR>';
	$extra['second_col'] .= '<TD colspan=2><INPUT type=checkbox name=elements[ytd_tardies] value=Y>'._('Other Attendance Year-to-date').': <SELECT name="ytd_tardies_code">';
	foreach($attendance_codes as $code)
		$extra['second_col'] .= '<OPTION value='.$code['ID'].'>'.$code['SHORT_NAME'].'</OPTION>';
	$extra['second_col'] .= '</SELECT></TD>';
	$extra['second_col'] .= '</TR><TR>';
	$extra['second_col'] .= '<TD colspan=2><INPUT type=checkbox name=elements[mp_tardies] value=Y>'._('Other Attendance this quarter').': <SELECT name="mp_tardies_code">';
	foreach($attendance_codes as $code)
		$extra['second_col'] .= '<OPTION value='.$code['ID'].'>'.$code['SHORT_NAME'].'</OPTION>';
	$extra['second_col'] .= '</SELECT></TD>';
	$extra['second_col'] .= '</TR><TR>';
$extra['second_col'] .= '</TR>';
$extra['second_col'] .= '</TABLE></TD></TR>';

$extra['second_col'] .= '<TR><TD align='.ALIGN_RIGHT.'>'._('Marking Periods').'</TD><TD><TABLE>';

$mps_RET = DBGet(DBQuery("SELECT SEMESTER_ID,MARKING_PERIOD_ID,SHORT_NAME FROM SCHOOL_QUARTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' ORDER BY SORT_ORDER"),array(),array('SEMESTER_ID'));

foreach($mps_RET as $sem=>$quarters)
{
	$extra['second_col'] .= '<TR>';
	foreach($quarters as $qtr)
	{
		$pro = GetChildrenMP('PRO',$qtr['MARKING_PERIOD_ID']);
		if($pro)
		{
			$pros = explode(',',str_replace("'",'',$pro));
			foreach($pros as $pro)
				$extra['second_col'] .= '<TD><INPUT type=checkbox name=mp_arr[] value='.$pro.'>'.GetMP($pro,'SHORT_NAME').'</TD>';
		}
		$extra['second_col'] .= '<TD><INPUT type=checkbox name=mp_arr[] value='.$qtr['MARKING_PERIOD_ID'].'>'.$qtr['SHORT_NAME'].'</TD>';
	}
	$extra['second_col'] .= '<TD><INPUT type=checkbox name=mp_arr[] value=E'.$sem.'>'.GetMP($sem,'SHORT_NAME').' '._('Exam').'</TD><TD><INPUT type=checkbox name=mp_arr[] value='.$sem.'>'.GetMP($sem,'SHORT_NAME').'</TD>';
	$extra['second_col'] .= '</TR>';
}
$extra['second_col'] .= '</TABLE>';
$extra['secont_col'] .= '</TD>';
$fy_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM SCHOOL_YEARS WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."'"));
$extra['second_col'] .= '<TD><INPUT type=checkbox name=mp_arr[] value='.$fy_RET[1]['MARKING_PERIOD_ID'].'>FY</TD>';

$extra['second_col'] .= '</TR></TABLE></fieldset>';
if($_REQUEST['modfunc']!='gradelist')
{
	Widgets('document_template');
	Widgets('mailing_labels');
}

/*
$extra['SELECT'] .= ",rpg.TITLE as GRADE_TITLE,sg1.COMMENT as COMMENT_TITLE,sg1.STUDENT_ID,sg1.COURSE_PERIOD_ID,sg1.MARKING_PERIOD_ID,c.TITLE as COURSE_TITLE,rc_cp.TITLE AS TEACHER,sp.SORT_ORDER ";
if($_REQUEST['elements']['period_absences'])
	$extra['SELECT'] .=	" ,(SELECT count(*) FROM ATTENDANCE_PERIOD ap,ATTENDANCE_CODES ac 
					WHERE ac.ID=ap.ATTENDANCE_CODE AND ac.STATE_CODE='A' AND ap.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID AND ap.STUDENT_ID=ssm.STUDENT_ID) AS YTD_ABSENCES,
				(SELECT count(*) FROM ATTENDANCE_PERIOD ap,ATTENDANCE_CODES ac 
					WHERE ac.ID=ap.ATTENDANCE_CODE AND ac.STATE_CODE='A' AND ap.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID AND sg1.MARKING_PERIOD_ID=ap.MARKING_PERIOD_ID AND ap.STUDENT_ID=ssm.STUDENT_ID) AS MP_ABSENCES ";
$extra['FROM'] .= ",STUDENT_REPORT_CARD_GRADES sg1 LEFT OUTER JOIN REPORT_CARD_GRADES rpg ON (rpg.ID=sg1.REPORT_CARD_GRADE_ID),
					COURSE_PERIODS rc_cp,COURSES c,SCHOOL_PERIODS sp";
$extra['WHERE'] .= " AND sg1.MARKING_PERIOD_ID IN (".$mp_list.")
					AND rc_cp.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID AND c.COURSE_ID = rc_cp.COURSE_ID AND sg1.STUDENT_ID=ssm.STUDENT_ID AND sp.PERIOD_ID=rc_cp.PERIOD_ID";
$extra['ORDER'] .= ",sp.SORT_ORDER,c.TITLE";
$extra['functions']['TEACHER'] = '_makeTeacher';
*/


$extra['SELECT'] .= ",sg1.GRADE_TITLE,sg1.PERCENT,sg1.COMMENT AS COMMENT_TITLE,sg1.STUDENT_ID,sg1.COURSE_TITLE,sg1.MARKING_PERIOD_ID,sg1.TEACHER,sp.SORT_ORDER";
if($_REQUEST['elements']['period_absences'])
{
	$extra['SELECT'] .=	" ,(SELECT count(*) FROM ATTENDANCE_PERIOD ap,ATTENDANCE_CODES ac 
					WHERE ac.ID=ap.ATTENDANCE_CODE AND ac.STATE_CODE='A' AND ap.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID AND ap.STUDENT_ID=ssm.STUDENT_ID) AS YTD_ABSENCES,
				(SELECT count(*) FROM ATTENDANCE_PERIOD ap,ATTENDANCE_CODES ac 
					WHERE ac.ID=ap.ATTENDANCE_CODE AND ac.STATE_CODE='A' AND ap.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID AND sg1.MARKING_PERIOD_ID=ap.MARKING_PERIOD_ID AND ap.STUDENT_ID=ssm.STUDENT_ID) AS MP_ABSENCES ";
}
$extra['FROM'] .= ",STUDENT_REPORT_CARD_GRADES sg1 LEFT OUTER JOIN COURSE_PERIODS rc_cp ON (rc_cp.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID) LEFT OUTER JOIN SCHOOL_PERIODS sp ON (sp.PERIOD_ID=rc_cp.PERIOD_ID)";
$extra['WHERE'] .= " AND sg1.MARKING_PERIOD_ID IN (".$mp_list.") AND sg1.STUDENT_ID=ssm.STUDENT_ID ";
$extra['ORDER'] .= ",sp.SORT_ORDER,sg1.COURSE_TITLE";

$extra['group']	= array('STUDENT_ID');
if($_REQUEST['mailing_labels']=='Y')
	$extra['group'][] = 'ADDRESS_ID';
$extra['group'][] = 'COURSE_TITLE';
$extra['group'][] = 'MARKING_PERIOD_ID';

/*Widgets('course');
Widgets('gpa');
Widgets('class_rank');
Widgets('letter_grade');
*/
Widgets('all');

if($_REQUEST['modfunc']!='gradelist')
	$extra['action'] .= "&_FOCUS_PDF=true";
else
	$extra['action'] .= '&modfunc=gradelist';
$extra['force_search'] = true;

if($_REQUEST['delete']=='true')
{
	if(DeletePrompt(_('final grade')))
	{
		DBQuery("DELETE FROM STUDENT_REPORT_CARD_GRADES WHERE SYEAR='".UserSyear()."' AND STUDENT_ID='".$_REQUEST['student_id']."' AND COURSE_PERIOD_ID='".$_REQUEST['course_period_id']."' AND MARKING_PERIOD_ID='".$_REQUEST['marking_period_id']."'");
		$_REQUEST['delete'] = 'false';
	}
}

if((!$_REQUEST['search_modfunc'] || $_REQUEST['search_modfunc']=='search' || $_FOCUS['modules_search']) && $_REQUEST['delete']!='true')
{
	DrawHeader(ProgramTitle());

	$extra['new'] = true;
	Search('student_id',$extra);
}
elseif($_REQUEST['delete']!='true')
{
	if(!$_REQUEST['mp_arr'])
		BackPrompt(_('You must choose at least one marking period'));
	
	$RET = GetStuList($extra);
		
	// GET THE COMMENTS
	if($_REQUEST['elements']['comments'])
		$comments_RET = DBGet(DBQuery("SELECT ID,TITLE,SUBSTR(TITLE,1,2) AS SHORT_NAME FROM REPORT_CARD_COMMENTS WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' ORDER BY TITLE"),array(),array('SHORT_NAME'));

	if($_REQUEST['elements']['mp_tardies']=='Y' || $_REQUEST['elements']['ytd_tardies']=='Y')
	{
		// GET THE ATTENDANCE
		unset($extra);
		$extra['SELECT_ONLY'] .= "ap.SCHOOL_DATE,ap.COURSE_PERIOD_ID,ac.ID AS ATTENDANCE_CODE,ap.MARKING_PERIOD_ID,ssm.STUDENT_ID ";
		$extra['FROM'] .= ",ATTENDANCE_CODES ac,ATTENDANCE_PERIOD ap ";
		$extra['WHERE'] .= " AND ac.ID=ap.ATTENDANCE_CODE AND (ac.DEFAULT_CODE!='Y' OR ac.DEFAULT_CODE IS NULL) AND ac.SYEAR=ssm.SYEAR AND ap.STUDENT_ID=ssm.STUDENT_ID";
		$extra['group'][] = 'STUDENT_ID';
		$extra['group'][] = 'ATTENDANCE_CODE';
		$extra['group'][] = 'MARKING_PERIOD_ID';
		Widgets('all');
		
		$attendance_RET = GetStuList($extra);
	}

	if($_REQUEST['elements']['mp_absences']=='Y' || $_REQUEST['elements']['ytd_absences']=='Y')
	{
		// GET THE DAILY ATTENDANCE
		unset($extra);
		$extra['SELECT_ONLY'] .= "ad.SCHOOL_DATE,ad.MARKING_PERIOD_ID,ad.STATE_VALUE,ssm.STUDENT_ID ";
		$extra['FROM'] .= ",ATTENDANCE_DAY ad ";
		$extra['WHERE'] .= " AND ad.STUDENT_ID=ssm.STUDENT_ID AND ad.SYEAR=ssm.SYEAR AND (ad.STATE_VALUE='0.0' OR ad.STATE_VALUE='.5') AND ad.SCHOOL_DATE<='".GetMP($last_mp,'END_DATE')."'";
		$extra['group'][] = 'STUDENT_ID';
		$extra['group'][] = 'MARKING_PERIOD_ID';
		Widgets('all');
		
		$attendance_day_RET = GetStuList($extra);
	}
	
	if($_REQUEST['elements']['average_percent_grade']=='Y' || $_REQUEST['elements']['gpa']=='Y' || $_REQUEST['elements']['weighted_gpa']=='Y')
	{
		// GET THE GPA
		unset($extra);
		$extra['SELECT_ONLY'] .= 'ssm.STUDENT_ID,sgc.GPA,sgc.WEIGHTED_GPA,sgc.CLASS_RANK,'."(SELECT AVG(asrcg.PERCENT)/100 FROM STUDENT_REPORT_CARD_GRADES asrcg WHERE asrcg.STUDENT_ID=ssm.STUDENT_ID AND asrcg.MARKING_PERIOD_ID='$last_mp') AS AVERAGE_PERCENT_GRADE";
		$extra['functions']['AVERAGE_PERCENT_GRADE'] = 'Percent';
		$extra['functions']['GPA'] = $extra['functions']['WEIGHTED_GPA'] = '_roundGPA';

		if(strpos($extra['FROM'],'STUDENT_GPA_CALCULATED sgc')===false)
		{
			$extra['FROM'] .= " LEFT OUTER JOIN STUDENT_GPA_CALCULATED sgc ON (sgc.STUDENT_ID=ssm.STUDENT_ID AND sgc.MARKING_PERIOD_ID='".$last_mp."')";
		}
		$extra['group'][] = 'STUDENT_ID';
		Widgets('all');
		
		$gpa_RET = GetStuList($extra);
	}

	if($_REQUEST['elements']['ilp']=='Y')
	{
		// GET THE ILP
		unset($extra);
		$extra['SELECT_ONLY'] .= 'ssm.STUDENT_ID,smc.COMMENT';

		$extra['FROM'] .= " LEFT OUTER JOIN STUDENT_MP_COMMENTS smc ON (smc.STUDENT_ID=ssm.STUDENT_ID AND smc.MARKING_PERIOD_ID='".$last_mp."')";

		$extra['group'][] = 'STUDENT_ID';
		Widgets('all');
		
		$ilp_RET = GetStuList($extra);
	}

	if(count($RET))
	{
		if($_REQUEST['modfunc']=='gradelist')
		{
			DrawHeader(ProgramTitle());
			if($_REQUEST['stuid'])
				DrawStudentHeader();
			$i = 0;
			foreach($RET as $student_id=>$course_periods)
			{
				$course_period_id = key($course_periods);
				$grades_RET[$i+1]['FULL_NAME'] = $course_periods[$course_period_id][key($course_periods[$course_period_id])][1]['FULL_NAME'];

				$columns = array('FULL_NAME'=>_('Student'),'COURSE_TITLE'=>_('Course'),'ABSENCES'=>_('Abs'));
				// .'<BR>'._('YTD').' / '._('MP')
				if($extra['columns_after'])
				{
					$columns += $extra['columns_after'];
					foreach($extra['columns_after'] as $column=>$title)
						$grades_RET[$i+1][$column] = $course_periods[$course_period_id][key($course_periods[$course_period_id])][1][$column];
				}
				$grades_RET[$i+1]['bgcolor'] = 'FFFFFF';
				foreach($course_periods as $course_period_id=>$mps)
				{
					$i++;
					$grades_RET[$i]['STUDENT_ID'] = $student_id;
					$grades_RET[$i]['COURSE_PERIOD_ID'] = $course_period_id;
					$grades_RET[$i]['MARKING_PERIOD_ID'] = key($mps);

					$grades_RET[$i]['COURSE_TITLE'] = $mps[key($mps)][1]['COURSE_TITLE'];
					foreach($_REQUEST['mp_arr'] as $mp)
					{
						if($mps[$mp])
						{
							$grades_RET[$i][$mp] = ' ';
							if($_REQUEST['elements']['percent']=='Y' && $mps[$mp][1]['PERCENT'])
								$grades_RET[$i][$mp] = $mps[$mp][1]['PERCENT'].'% ';
							if($_REQUEST['elements']['grade']=='Y')
								$grades_RET[$i][$mp] .= $mps[$mp][1]['GRADE_TITLE'];	
						}
					}
					$grades_RET[$i]['TEACHER'] = $mps[$last_mp][1]['TEACHER'];
					$grades_RET[$i]['COMMENT'] = $mps[$last_mp][1]['COMMENT_TITLE'];
					$grades_RET[$i]['ABSENCES'] = $mps[$last_mp][1]['YTD_ABSENCES'];
				}
			}

			if($_REQUEST['elements']['teacher']=='Y')
				$columns += array('TEACHER'=>_('Teacher'));
			if($_REQUEST['elements']['period_absences']=='Y')
				$columns += array('ABSENCES'=>_('Abs').'<BR>'._('YTD').' / '._('MP'));
			if($extra['columns_after'])
				$columns += $extra['columns_after'];
			foreach($_REQUEST['mp_arr'] as $mp)
				$columns[$mp] = GetMP($mp);
			if($_REQUEST['elements']['comments']=='Y')
				$columns['COMMENT'] = _('Comment');

			if(count($_REQUEST['mp_arr'])==1)
			{
				$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&delete=true";
				$link['remove']['variables'] = array('student_id'=>'STUDENT_ID','course_period_id'=>'COURSE_PERIOD_ID','marking_period_id'=>'MARKING_PERIOD_ID');
			}
			ListOutput($grades_RET,$columns,'','',$link);
		}
		else
		{
			$handle = PDFStart();
			foreach($RET as $student_id=>$course_periods)
			{
				$comments_arr = array();
				if($_REQUEST['mailing_labels']=='Y')
				{
					foreach($course_periods as $address_id=>$address)
					{
						unset($_FOCUS['DrawHeader']);
						unset($grades_RET);
						$i = 0;
						foreach($address as $course_period_id=>$mps)
						{
							$i++;
							$grades_RET[$i]['COURSE_TITLE'] = $mps[key($mps)][1]['COURSE_TITLE'];
							foreach($_REQUEST['mp_arr'] as $mp)
							{						
								if($mps[$mp])
								{
									$grades_RET[$i][$mp] = '';
									if($_REQUEST['elements']['percent']=='Y' && $mps[$mp][1]['PERCENT'])
										$grades_RET[$i][$mp] = $mps[$mp][1]['PERCENT'].'% ';	
									if($_REQUEST['elements']['grade']=='Y')
										$grades_RET[$i][$mp] .= $mps[$mp][1]['GRADE_TITLE'];	
									$last_mp = $mp;
								}
							}
							$grades_RET[$i]['TEACHER'] = $mps[$last_mp][1]['TEACHER'];
							$grades_RET[$i]['COMMENT'] = $mps[$last_mp][1]['COMMENT_TITLE'];
							if($_REQUEST['elements']['comments']=='Y' && is_numeric(ereg_replace('[^0-9]+','',$grades_RET[$i]['COMMENT'])))
							{
								$comments = explode(',',ereg_replace('[^0-9,]+','',ereg_replace(',? +',',',$grades_RET[$i]['COMMENT'])));
								if(count($comments))
								{
									foreach($comments as $comment)
										$comments_arr[$comment] = true;
								}
							}
							$grades_RET[$i]['ABSENCES'] = $mps[$last_mp][1]['YTD_ABSENCES'].' / '.$mps[$last_mp][1]['MP_ABSENCES'];
						}
						
						$columns = array('COURSE_TITLE'=>_('Course'));
						if($_REQUEST['elements']['teacher']=='Y')
							$columns += array('TEACHER'=>_('Teacher'));
						if($_REQUEST['elements']['period_absences']=='Y')
							$columns += array('ABSENCES'=>_('Abs'));
						// .'<BR>'._('YTD').' / '._('MP')
						if($extra['columns_after'])
							$columns += $extra['columns_after'];
						foreach($_REQUEST['mp_arr'] as $mp)
							$columns[$mp] = GetMP($mp);
						if($_REQUEST['elements']['comments']=='Y')
							$columns['COMMENT'] = _('Comment');
						
						echo '<BR><BR><BR>';
						DrawHeader(Config('TITLE').' '._('Report Card'));
						DrawHeader($address[$course_period_id][key($address[$course_period_id])][1]['FULL_NAME'],$address[$course_period_id][key($address[$course_period_id])][1]['STUDENT_ID']);
						DrawHeader($address[$course_period_id][key($address[$course_period_id])][1]['GRADE_ID'],GetSchool(UserSchool()));
						$count_lines = 3;
						
						unset($grades_header);
						if($_REQUEST['elements']['average_percent_grade']=='Y')
						{
							$grades_header[] = _('Average Percent Grade').': '.$gpa_RET[$student_id][1]['AVERAGE_PERCENT_GRADE'];
						}
						if($_REQUEST['elements']['gpa']=='Y')
						{
							$grades_header[] = _('GPA').': '.$gpa_RET[$student_id][1]['GPA'];
						}
						if($_REQUEST['elements']['weighted_gpa']=='Y')
						{
							$grades_header[] = _('Weighted GPA').': '.$gpa_RET[$student_id][1]['WEIGHTED_GPA'];
						}
						if(count($grades_header))
						{
							DrawHeader($grades_header[0],$grades_header[1],$grades_header[2],'','',true);
							$count_lines++;
						}
						if($_REQUEST['elements']['mp_absences']=='Y')
						{
							$count = 0;
							foreach($attendance_day_RET[$student_id][$last_mp] as $abs)
								$count += ($abs['STATE_VALUE']-1)*-1;
							$mp_absences = _('Absences in').' '.GetMP($last_mp,'TITLE').': '.$count;
						}
						if($_REQUEST['elements']['ytd_absences']=='Y')
						{
							$count = 0;
							foreach($attendance_day_RET[$student_id] as $mp_abs)
							{
								foreach($mp_abs as $abs)
									$count += ($abs['STATE_VALUE']-1)*-1;
							}
							DrawHeader(_('Absences this year').': '.$count,$mp_absences,'','','',true);
							$count_lines++;
						}
						elseif($_REQUEST['elements']['mp_absences']=='Y')
						{
							DrawHeader($mp_absences,'','','','',true);
							$count_lines++;
						}

						if($_REQUEST['elements']['mp_tardies']=='Y')
						{
							$count = 0;
							foreach($attendance_RET[$student_id][$_REQUEST['mp_tardies_code']][$last_mp] as $abs)
								$count++;
							$mp_tardies = _('Tardy in').' '.GetMP($last_mp,'TITLE').': '.$count;
						}
						if($_REQUEST['elements']['ytd_tardies']=='Y')
						{
							$count = 0;
							foreach($attendance_RET[$student_id][$_REQUEST['ytd_tardies_code']] as $mp_abs)
							{
								foreach($mp_abs as $abs)
									$count++;
							}
							DrawHeader(_('Tardy this year').': '.$count,$mp_tardies,'','','',true);
							$count_lines++;
						}
						elseif($_REQUEST['elements']['mp_tardies']=='Y')
						{
							DrawHeader($mp_tardies,'','','','',true);
							$count_lines++;
						}

						DrawHeader(ProperDate(DBDate()));
						$count_lines++;
						for($i=$count_lines;$i<=6;$i++)
							echo '<BR>';
						
						echo '<TABLE><TR><TD width=50> &nbsp; </TD><TD width=300>'.$address[$course_period_id][key($address[$course_period_id])][1]['MAILING_LABEL'].'</TD></TR></TABLE>';
						
						ListOutput($grades_RET,$columns,'','',array(),array(),array('print'=>false));
						if($_REQUEST['elements']['comments']=='Y' && count($comments_arr))
						{
							foreach($comments_arr as $comment=>$true)
							{
								if(!$comments_RET[$comment][1]['TITLE'] && !$comments_RET['0'.$comment][1]['TITLE'])
									unset($comments_arr[$comment]);
							}
						}					
						if($_REQUEST['elements']['comments']=='Y' && count($comments_arr))
						{
							echo '<TABLE width=100%><TR><TD colspan=2><b>'._('Explanation of Comment Codes').'</b></TD>';
							$i=3;
							foreach($comments_arr as $comment=>$true)
							{
								if($i%3==0)
									echo '</TR><TR>';
								if(!$comments_RET[$comment][1]['TITLE'] && $comments_RET['0'.$comment][1]['TITLE'])
									$comment = '0'.$comment;
								echo '<TD>'.$comments_RET[$comment][1]['TITLE'].'</TD>';
								$i++;
							}
							echo '</TR></TABLE>';
						}
						if($_REQUEST['elements']['ilp'])
						{
							echo '<CENTER><BR><TABLE width=90%><TR><TD><b>'._('Individual Learning Plan').'</b><BR><BR>';
							echo $ilp_RET[$student_id][1]['COMMENT'];
							echo '</TD></TR></TABLE></CENTER>';
							
						}
						DrawTemplateHeaders($student_id,'footer');
						echo '<!-- NEW PAGE -->';
					}
				}
				else
				{
					unset($_FOCUS['DrawHeader']);
					unset($grades_RET);
					$i = 0;
					foreach($course_periods as $course_period_id=>$mps)
					{
						$i++;
						$grades_RET[$i]['COURSE_TITLE'] = $mps[key($mps)][1]['COURSE_TITLE'];
						foreach($_REQUEST['mp_arr'] as $mp)
						{						
							if($mps[$mp])
							{
								$grades_RET[$i][$mp] = '';
								if($_REQUEST['elements']['percent']=='Y' && $mps[$mp][1]['PERCENT'])
									$grades_RET[$i][$mp] = $mps[$mp][1]['PERCENT'].'% ';
								if($_REQUEST['elements']['grade']=='Y')
									$grades_RET[$i][$mp] .= $mps[$mp][1]['GRADE_TITLE'];	

								$last_mp = $mp;
							}
						}
						$grades_RET[$i]['TEACHER'] = $mps[$last_mp][1]['TEACHER'];
						$grades_RET[$i]['ABSENCES'] = $mps[$last_mp][1]['YTD_ABSENCES'];
						// .' / '.$mps[$last_mp][1]['MP_ABSENCES']
						$grades_RET[$i]['COMMENT'] = $mps[$last_mp][1]['COMMENT_TITLE'];
						if($_REQUEST['elements']['comments']=='Y' && is_numeric(ereg_replace('[^0-9]+','',$grades_RET[$i]['COMMENT'])))
						{
							$comments = explode(',',ereg_replace('[^0-9,]+','',ereg_replace(',? +',',',$grades_RET[$i]['COMMENT'])));
							if(count($comments))
							{
								foreach($comments as $comment)
									$comments_arr[$comment] = true;
							}
						}
					}
					
					$columns = array('COURSE_TITLE'=>_('Course'));
					if($_REQUEST['elements']['teacher']=='Y')
						$columns += array('TEACHER'=>_('Teacher'));
					if($_REQUEST['elements']['period_absences']=='Y')
						$columns += array('ABSENCES'=>_('Abs'));
					//.'<BR>'._('YTD').' / '._('MP'));
					if($extra['columns_after'])
						$columns += $extra['columns_after'];
					if(count($_REQUEST['mp_arr'])>4)
						$mp_TITLE = 'SHORT_NAME';
					else
						$mp_TITLE = 'TITLE';
					foreach($_REQUEST['mp_arr'] as $mp)
						$columns[$mp] = GetMP($mp,$mp_TITLE);
					if($_REQUEST['elements']['comments']=='Y')
						$columns['COMMENT'] = _('Comment');
					
					DrawHeader(Config('TITLE').' '._('Report Card'));
					DrawHeader($course_periods[$course_period_id][key($course_periods[$course_period_id])][1]['FULL_NAME'],$course_periods[$course_period_id][key($course_periods[$course_period_id])][1]['STUDENT_ID']);
					DrawHeader($course_periods[$course_period_id][key($course_periods[$course_period_id])][1]['GRADE_ID'],GetSchool(UserSchool()));
					unset($grades_header);
					if($_REQUEST['elements']['average_percent_grade']=='Y')
					{
						$grades_header[] = _('Average Percent Grade').': '.$gpa_RET[$student_id][1]['AVERAGE_PERCENT_GRADE'];
					}
					if($_REQUEST['elements']['gpa']=='Y')
					{
						$grades_header[] = _('GPA').': '.$gpa_RET[$student_id][1]['GPA'];
					}
					if($_REQUEST['elements']['weighted_gpa']=='Y')
					{
						$grades_header[] = _('Weighted GPA').': '.$gpa_RET[$student_id][1]['WEIGHTED_GPA'];
					}
					if(count($grades_header))
					{
						DrawHeader($grades_header[0],$grades_header[1],$grades_header[2],'','',true);
						$count_lines++;
					}

					if($_REQUEST['elements']['mp_absences']=='Y')
					{
						$count = 0;
						foreach($attendance_day_RET[$student_id][$last_mp] as $abs)
							$count += ($abs['STATE_VALUE']-1)*-1;
						$mp_absences = _('Absences in').' '.GetMP($last_mp,'TITLE').': '.$count;
					}
					if($_REQUEST['elements']['ytd_absences']=='Y')
					{
						$count = 0;
						foreach($attendance_day_RET[$student_id] as $mp_abs)
						{
							foreach($mp_abs as $abs)
								$count += ($abs['STATE_VALUE']-1)*-1;
						}
						DrawHeader(_('Absences this year').': '.$count,$mp_absences,'','','',true);
						$count_lines++;
					}
					elseif($_REQUEST['elements']['mp_absences']=='Y')
					{
						DrawHeader($mp_absences,'','','','',true);
						$count_lines++;
					}

					if($_REQUEST['elements']['mp_tardies']=='Y')
					{
						$count = 0;
						foreach($attendance_RET[$student_id][$_REQUEST['mp_tardies_code']][$last_mp] as $abs)
							$count++;
						$mp_tardies = _('Tardy in').' '.GetMP($last_mp,'TITLE').': '.$count;
					}
					if($_REQUEST['elements']['ytd_tardies']=='Y')
					{
						$count = 0;
						foreach($attendance_RET[$student_id][$_REQUEST['ytd_tardies_code']] as $mp_abs)
						{
							foreach($mp_abs as $abs)
								$count++;
						}
						DrawHeader(_('Tardy this year').': '.$count,$mp_tardies,'','','',true);
						$count_lines++;
					}
					elseif($_REQUEST['elements']['mp_tardies']=='Y')
					{
						DrawHeader($mp_tardies,'','','','',true);
						$count_lines++;
					}
					
					echo '<BR>';
					ListOutput($grades_RET,$columns,'','',array(),array(),array('print'=>false,'center'=>false));
					if($_REQUEST['elements']['comments']=='Y' && count($comments_arr))
					{
						foreach($comments_arr as $comment=>$true)
						{
							if(!$comments_RET[$comment][1]['TITLE'] && !$comments_RET['0'.$comment][1]['TITLE'])
								unset($comments_arr[$comment]);
						}
					}					
					if($_REQUEST['elements']['comments']=='Y' && count($comments_arr))
					{
						echo '<TABLE width=100%><TR><TD colspan=2><b>'._('Explanation of Comment Codes').'</b></TD>';
						$i=3;
						foreach($comments_arr as $comment=>$true)
						{
							if($i%3==0)
								echo '</TR><TR>';
							if(!$comments_RET[$comment][1]['TITLE'] && $comments_RET['0'.$comment][1]['TITLE'])
								$comment = '0'.$comment;
							echo '<TD>'.$comments_RET[$comment][1]['TITLE'].'</TD>';
							$i++;
						}
						echo '</TR></TABLE>';
					}
					if($_REQUEST['elements']['ilp'])
					{
						echo '<CENTER><BR><TABLE width=90%><TR><TD><b>'._('Teacher MP Comments').'</b><BR><BR>';
						echo $ilp_RET[$student_id][1]['COMMENT'];
						echo '</TD></TR></TABLE></CENTER>';
						
					}
					DrawTemplateHeaders($student_id,'footer');
					echo '<!-- NEW PAGE -->';
				}
			}
			PDFStop($handle);
		}
	}
	else
		BackPrompt(_('No Students were found.'));
}

function _makeTeacher($teacher,$column)
{
	return substr($teacher,strrpos(str_replace(' - ',' ^ ',$teacher),'^')+2);
}

function _roundGPA($gpa,$column)
{
	return round($gpa,3);
}

?>